﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using UnityEngine;

public class Localization : Singleton<Localization>
{
    [CompilerGenerated]
    private static Func<KeyValuePair<string, LanguageData>, string> <>f__am$cache2;
    private const string fileExtension = ".localization";
    private static string language;
    private Dictionary<string, LanguageData> languageDataLookup = new Dictionary<string, LanguageData>();

    private void Awake()
    {
        foreach (string str in Directory.GetFiles(Application.streamingAssetsPath, "*.localization", SearchOption.AllDirectories))
        {
            Collection collection = this.LoadCollection(str);
            if (collection != null)
            {
                string language = collection.language;
                if (!this.languageDataLookup.ContainsKey(language))
                {
                    this.languageDataLookup.Add(language, new LanguageData());
                }
                LanguageData data = this.languageDataLookup[language];
                foreach (Element element in collection.elements)
                {
                    data.lookup[element.id] = element.value;
                }
            }
        }
        Localization.language = Language;
    }

    public static string[] GetLanguages()
    {
        if (<>f__am$cache2 == null)
        {
            <>f__am$cache2 = x => x.Key;
        }
        return Singleton<Localization>.Instance.languageDataLookup.Select<KeyValuePair<string, LanguageData>, string>(<>f__am$cache2).ToArray<string>();
    }

    public static string GetText(string id, string defaultText)
    {
        LanguageData data;
        if (string.IsNullOrEmpty(id))
        {
            return defaultText;
        }
        Singleton<Localization>.Instance.languageDataLookup.TryGetValue(Language, out data);
        if (data == null)
        {
            return defaultText;
        }
        string str = data.GetValue(id);
        if (string.IsNullOrEmpty(str))
        {
            return (string.IsNullOrEmpty(defaultText) ? id : defaultText);
        }
        return str;
    }

    [RuntimeInitializeOnLoadMethod]
    private static void Init()
    {
        Singleton<Localization>.Instance.enabled = true;
    }

    private Collection LoadCollection(string path)
    {
        Collection collection = null;
        try
        {
            JObject obj2 = JObject.Parse(File.ReadAllText(path));
            collection = new Collection();
            string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path);
            char ch = '@';
            collection.language = !fileNameWithoutExtension.Contains<char>(ch) ? fileNameWithoutExtension : fileNameWithoutExtension.Split(new char[] { ch }).Last<string>();
            string newValue = Path.GetDirectoryName(path).Replace(@"\", "/").Replace(Application.streamingAssetsPath + "/", string.Empty);
            string str4 = newValue + "/";
            IEnumerator<JProperty> enumerator = obj2.Children<JProperty>().GetEnumerator();
            try
            {
                while (enumerator.MoveNext())
                {
                    JProperty current = enumerator.Current;
                    Element item = new Element {
                        id = current.Name
                    };
                    if (current.Name == "$prefix")
                    {
                        str4 = current.Value.ToString().Replace("$folderName", newValue);
                    }
                    if (current.Name == "$language")
                    {
                        collection.language = current.Value.ToString();
                    }
                    if (!string.IsNullOrEmpty(str4))
                    {
                        item.id = str4 + item.id;
                    }
                    item.value = current.Value.ToString();
                    collection.elements.Add(item);
                }
            }
            finally
            {
                if (enumerator == null)
                {
                }
                enumerator.Dispose();
            }
        }
        catch (Exception exception)
        {
            object[] args = new object[] { exception.Message, path };
            Debug.LogErrorFormat("Failed to load localization : {0} (at '{1}')", args);
        }
        return collection;
    }

    public static void Unload()
    {
        UnityEngine.Object.Destroy(Singleton<Localization>.Instance.gameObject);
    }

    public static string Language
    {
        get
        {
            if (language == null)
            {
                language = PlayerPrefs.GetString("Localization.language", "简体中文");
            }
            return language;
        }
        set
        {
            PlayerPrefs.SetString("Localization.language", value);
            language = value;
        }
    }

    [Serializable]
    public class Collection
    {
        public List<Localization.Element> elements = new List<Localization.Element>();
        public string language;
    }

    [Serializable, StructLayout(LayoutKind.Sequential)]
    public struct Element
    {
        public string id;
        public string value;
    }

    public class LanguageData
    {
        public Dictionary<string, string> lookup = new Dictionary<string, string>();

        public string GetValue(string id)
        {
            string str;
            this.lookup.TryGetValue(id, out str);
            return str;
        }
    }
}

